package org.example.struct.array2;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/10/31 09:00
 * @apiNote
 */

public class Test00969PancakeSort {

    public static void main(String[] args) {
        int[] arr ={3,2,4,1};
        List<Integer> res = test(arr);
        System.out.println(JSON.toJSON(res));
    }

    /**
     *
     * @param arr
     * @return
     */
    public static List<Integer> test(int[] arr) {
        List<Integer> res = new ArrayList<>();
        for (int i = arr.length; i > 1; i--) {
            int index = 0;
            for (int j = 1; j < i; j++) {
                if(arr[j]>=arr[index]){
                    index = j;
                }
            }
            if(index ==i-1){
                continue;
            }
            reverse(arr,0,index);
            reverse(arr,0,i-1);
            res.add(index+1);
            res.add(i);
        }
        return res;
    }

    private static void reverse(int[] arr, int i, int j) {
        while (i < j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }

}
